<?php
require_once 'models/orm.inc';
require_once 'models/cms.inc';

class TCMSPageAssociationIterator extends TIteratorAdapter {
	private $_page_;
	
	public function __construct(IIterator $base, $page){
		parent::__construct($base);
		$this->_page_ = $page;
	}
	
	public function Item(){
		if ($this->iterator->Item()->MasterPage->ItemId() == $this->_page_)
			return $this->iterator->Item()->DetailPage;
		return $this->iterator->Item()->MasterPage;
	}
}

/**
 * 
 * @author kras
 * @property IListProvider $ClassAssigner 
 */

class TCMSPageAssociationModel extends TCandyModel {
	
/**
 * @var IListProvider
 */	
	protected $_ioc_class_assigner_;
	
	protected function afterConstruct(){
		$this->_conf_classname_ = 'TMSPageAssociation';
	}
	
	public function DeassociatePages(IItem $class, IItem $page, array $pages){
		$assoc = $this->getAssociations($class, $page->ItemId());
		foreach ($assoc as $p)
			if (in_array($p->DetailPage->Id, $pages) || in_array($p->MasterPage->Id,$pages)){
				$this->Delete($p);
			}
	}
	
	public function AssociatePages(IItem $class,IItem $page, array $pages){
		$assoc = $this->getAssociations($class, $page->ItemId());
		$old_master = array();
		$old_detail = array();
		foreach ($assoc as $p){
			if ($p->MasterPage->ItemId() != $page->ItemId())
				$old_master[] = $p->MasterPage->ItemId();
			if ($p->DetailPage->ItemId() != $page->ItemId())
				$old_detail[] = $p->DetailPage->ItemId();
		}

		foreach ($pages as $pid)
			if (!in_array($pid,$old_master) && !in_array($pid,$old_detail)){
				if ($class->Type == TMSPageAssociationType::AGREGATED){
					$old_agreg = $this->GetList(new TListOptions(array(
							new TFilterOption('Class.Type', TConditionType::C_EQUAL, TMSPageAssociationType::AGREGATED),
							new TFilterOption('DetailPage', TConditionType::C_EQUAL, $pid)
					)));
						
					foreach ($old_agreg as $p)
						$this->Delete($p);
				}
				$this->CreateItem(array('MasterPage'=>$page->ItemId(),'DetailPage'=>$pid, 'Class'=>$class->ItemId()));
			}
	}
	
	protected function postCreateItem(TCandyItem $item,array $data = array()){
		if ($this->ClassAssigner){
			$this->ClassAssigner->CreateItem(array('Page'=>$item->MasterPage->ItemId(),'Class'=>$item->Class->ItemId()));
		}
	}
	
	protected function getAssociations(IItem $class, $page){
		$filter = array(new TFilterOption('Class', TConditionType::C_EQUAL, $class->ItemId()));
		if ($class->Type == TMSPageAssociationType::BIDIRECTIONAL){
			$filter[] = new TCondition(TConditionType::C_OR, array(
					new TFilterOption('MasterPage', TConditionType::C_EQUAL, $page),
					new TFilterOption('DetailPage', TConditionType::C_EQUAL, $page)
			));
		} else 
			$filter[] = new TFilterOption('MasterPage', TConditionType::C_EQUAL, $page);
		return $this->GetList(new TListOptions($filter));
		
	}
	
	public function PageAssociations(IItem $class,IItem $subject){
		return new TCMSPageAssociationIterator($this->getAssociations($class, $subject->ItemId()),$subject->ItemId());
	}
	
	public function Container(IItem $subject){
		$assoc = $this->GetList(new TListOptions(array(
				new TFilterOption('Class.Type', TConditionType::C_EQUAL, TMSPageAssociationType::AGREGATED),
				new TFilterOption('DetailPage', TConditionType::C_EQUAL, $subject->ItemId())
				)));
		foreach ($assoc as $a)
			return $a->MasterPage;
		return null;
	}
} 
